﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Public Class DownloadsFilesEx
    Shared Sub New()
        Dim aSelectColumns As String() = {"Id", "ItemId", "SHA512Hash" _
                                        , "Size", "Name", "Directory" _
                                        , "Status", "PartCount" _
                                        , "Priority", "Started", "Completed" _
                                        , "PartsCompleted", "PartStatuses", "LastChanged"}
        ObjectPersister.DefineObject(GetType(Entities.Downloads.File), "Downloads_Files", aSelectColumns)
    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Downloads.File)
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)()
    End Function
    Public Shared Function GetAllByStatus(ByVal status As Entities.Downloads.Status) As Generic.List(Of Entities.Downloads.File)
        Dim sqlPar1 As New SqlCeParameter("@Status", CByte(status))
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)("Status=@Status", sqlPar1)
    End Function
    Public Shared Function GetAllByItemId(ByVal itemId As Integer) As Generic.List(Of Entities.Downloads.File)
        Dim sqlPar1 As New SqlCeParameter("@ItemId", itemId)
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)("ItemId=@ItemId", sqlPar1)
    End Function
    Public Shared Function GetAllByItemId(ByVal itemId As Integer, ByVal transactionScope As TransactionScope) As Generic.List(Of Entities.Downloads.File)
        Dim sqlPar1 As New SqlCeParameter("@ItemId", itemId)
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)("ItemId=@ItemId", transactionScope, sqlPar1)
    End Function
    Public Shared Function GetAllByDirectoryRecursive(ByVal directory As String) As Generic.List(Of Entities.Downloads.File)
        Dim sqlPar1 As New SqlCeParameter("@Directory", directory)
        Dim sqlPar2 As New SqlCeParameter("@Directory2", directory & "\%")
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)("Directory=@Directory OR Directory LIKE @Directory2", sqlPar1, sqlPar2)
    End Function
    Public Shared Function GetByDirectoryAndFilename(ByVal directory As String, ByVal name As String) As Entities.Downloads.File
        Dim sqlPar1 As New SqlCeParameter("@Directory", directory)
        Dim sqlPar2 As New SqlCeParameter("@Name", name)
        Return ObjectPersister.GetOne(Of Entities.Downloads.File)("Directory=@Directory AND Name=@Name", sqlPar1, sqlPar2)
    End Function
    Public Shared Function CountByItemId(ByVal itemId As Integer, ByVal transactionScope As TransactionScope) As Integer
        Dim sqlCmd As Command = Environment.NewCommand("SELECT COUNT(*) FROM Downloads_Files WHERE ItemId=@ItemId", transactionScope)
        sqlCmd.AddParameterWithValue("@ItemId", itemId)
        Dim aRet As Generic.List(Of Integer) = Environment.ExecuteCommandToIntegerList(sqlCmd)
        Return aRet(0)
    End Function
    Public Shared Function GetAllBySHA512Hash(ByVal sha512Hash As String) As Generic.List(Of Entities.Downloads.File)
        Dim strClause As String = "[SHA512Hash]=@Hash"
        Dim sqlPar1 As New SqlCeParameter("@Hash", sha512Hash)
        Return ObjectPersister.GetAll(Of Entities.Downloads.File)(strClause, sqlPar1)
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Downloads.File
        Dim strClause As String = "[Id]=@Id"
        Dim sqlPar1 As New SqlCeParameter("@Id", id)
        Return ObjectPersister.GetOne(Of Entities.Downloads.File)(strClause, sqlPar1)
    End Function
    Public Shared Function GetBySHA512HashAndSize(ByVal sha512Hash As String, ByVal size As Long) As Entities.Downloads.File
        Dim strClause As String = "[SHA512Hash]=@Hash AND [Size]=@Size"
        Dim sqlPar1 As New SqlCeParameter("@Hash", sha512Hash)
        Dim sqlPar2 As New SqlCeParameter("@Size", size)
        Return ObjectPersister.GetOne(Of Entities.Downloads.File)(strClause, sqlPar1, sqlPar2)
    End Function
    Public Shared Function GetByNameSizeAndLastChanged(ByVal name As String, ByVal size As Long, ByVal lastChanged As Date) As Entities.Downloads.File
        Dim strClause As String = "[Name]=@Name AND [Size]=@Size AND [LastChanged]=@LastChanged"
        Dim sqlPar1 As New SqlCeParameter("@Name", name)
        Dim sqlPar2 As New SqlCeParameter("@Size", size)
        Dim sqlPar3 As New SqlCeParameter("@LastChanged", lastChanged)
        Return ObjectPersister.GetOne(Of Entities.Downloads.File)(strClause, sqlPar1, sqlPar2, sqlPar3)
    End Function

    Public Shared Sub Save(ByVal file As Entities.Downloads.File, ByVal transactionScope As TransactionScope)
        If file.Status = Entities.Downloads.Status.Paused Then Beep()
        Dim clsCmd As New SaveCommand(Of Integer)("Downloads_Files", "Id", file.Id, 0)
        clsCmd.AddParam("Name", file.Name, False)
        clsCmd.AddParam("ItemId", file.ItemId, False)
        clsCmd.AddParam("SHA512Hash", file.SHA512Hash, False)
        clsCmd.AddParam("Size", file.Size, False)
        clsCmd.AddParam("Directory", file.Directory, False)
        clsCmd.AddParam("Status", file.Status, False)
        clsCmd.AddParam("PartCount", file.PartCount, False)
        clsCmd.AddParam("Priority", file.Priority, False)
        clsCmd.AddParam("Started", file.Started, False)
        clsCmd.AddParam("Completed", file.Completed, True)
        clsCmd.AddParam("PartsCompleted", file.PartsCompleted, False)
        clsCmd.AddParam("PartStatuses", file.PartStatuses, True)
        clsCmd.AddParam("LastChanged", file.LastChanged, True)
        file.Id = clsCmd.Save(transactionScope)
    End Sub

    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        Dim strSql As String = "DELETE FROM Downloads_Files WHERE [Id]=@Id"
        Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
        sqlCmd.AddParameterWithValue("@Id", id)
        Environment.ExecuteNonQuery(sqlCmd)
    End Sub
End Class
